# This file is part of Neuroinfo Toolkit.
#
# Neuroinfo Toolkit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Neuroinfo Toolkit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Neuroinfo Toolkit.  If not, see <http://www.gnu.org/licenses/>.

import unittest
from neuro.filesystem import *
from neuro.command import Command
from neuro.command import CommandFailedException
from neuro.exceptions import IllegalArgumentException

class TestCommand(unittest.TestCase):
	def setUp(self):
		pass

	def test_basic(self):
		## --- no arguments
		self.assertRaises(TypeError, Command)

		## --- bad argument
		self.assertRaises(IllegalArgumentException, Command, list())
		self.assertRaises(IllegalArgumentException, Command, "")
		
	def test_command(self):
		command = Command("echo foo")
		assert command.execute() is None
		self.assertEqual(command.getStdout(), "foo")
		self.assertEqual(command.getStatus(), 0)

	def test_pipeCommand(self):
		## --- successful pipe
		command = Command("echo foo | grep foo")
		assert command.execute() is None
		self.assertEqual(command.getStdout(), "foo")
		self.assertEqual(command.getStatus(), 0)

		## --- first part of pipe fails
		command = Command("cat ~/98ffgses34 | echo foo")
		self.assertRaises(CommandFailedException, command.execute)
		self.assertNotEqual(command.getStatus(), 0)

	def tearDown(self):
		pass

unittest.main()